package com.epoint.webapp.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.jasper.tagplugins.jstl.core.If;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.objenesis.instantiator.basic.NewInstanceInstantiator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.epoint.webapp.dao.ProductDAO;
import com.epoint.webapp.dao.ProductSalesDAO;
import com.epoint.webapp.dao.VentureChecklistDAO;
import com.epoint.webapp.entity.MapClass;
import com.epoint.webapp.entity.MapSubclass;
import com.epoint.webapp.entity.Member;
import com.epoint.webapp.entity.Product;
import com.epoint.webapp.entity.ProductSales;
import com.epoint.webapp.util.PageCount;
import com.google.gson.Gson;

@Controller
public class ProductController {
	ClassPathXmlApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");

	@RequestMapping(value = "/showAllProductDescribe", method = RequestMethod.GET)
	public ModelAndView showAllProductDescribe (HttpServletRequest request, HttpSession session){
		ModelAndView model = new ModelAndView();
		Member memberLogin = (Member)session.getAttribute("loginMember");
		if(memberLogin==null)
			model.setViewName("memberLogin");
		else{
			
			VentureChecklistDAO ventureChecklistDAO = (VentureChecklistDAO)context.getBean("ventureChecklistDAO");
			MapClass mapClass = new MapClass();
			mapClass.setId(1);
			mapClass.setAccount(memberLogin.getAccount());
			List<MapClass> getVentureCheckMenuList = ventureChecklistDAO.getMapClassList(mapClass);
			
			//memberLogin.setClassID(15);
			//List<MapSubclass> getVentureCheckListByMember= ventureChecklistDAO.getVentureCheckListByMember(memberLogin);
			//get data from request
			boolean flag = ventureChecklistDAO.checkVentureCheckListByMemberSubID(memberLogin.getAccount(),123);
			if(flag==true){
				model.setViewName("showAllProductDescribe");
				int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
				
				//set pager
				int pageSize = 10;
				ProductDAO productDAO = (ProductDAO)context.getBean("productDAO");
				int count = productDAO.countProductByMember(memberLogin);
				String pageUrl = "showAllProductDescribe";
				//PageCount(int pageNow, int pageSize, int count, String url, String endUrl)
				PageCount pageCount = new PageCount(pageNow, pageSize, count, pageUrl, "");
				pageCount.calculate();
				
				//getProduct
				List<Product> getProductList = new ArrayList<Product>();
				getProductList = productDAO.getProductList(memberLogin,pageCount.getStart(), pageSize);	
				
				model.addObject("page", pageCount);
				model.addObject("getVentureCheckMenuList",getVentureCheckMenuList);
				//model.addObject("getVentureCheckListByMember", getVentureCheckListByMember);
				model.addObject("getVentureCheckMenuListNow",getVentureCheckMenuList.get(0).getNow());
				model.addObject("getProductList1",getProductList);
				model.addObject("getProductList2", new Gson().toJson(getProductList));
			}else
				model.setViewName("redirect:/targetMarketDescribe");			
		}
		
		return model;
	}	
	
	@RequestMapping(value = "/addProduct", method = RequestMethod.POST)
	public ModelAndView addProduct (Product product, HttpServletRequest request, HttpSession session){
		ModelAndView model = new ModelAndView();
		Member memberLogin = (Member)session.getAttribute("loginMember");		
		if(memberLogin!=null){				
			
			product.setAccount(memberLogin.getAccount());
			ProductDAO productDAO = (ProductDAO)context.getBean("productDAO");
			productDAO.insertProduct1(product);	
			int count = productDAO.countProductByMember(memberLogin);	
			int page = count/10+1;
			model.setViewName("redirect:/showAllProductDescribe?p="+page);
		}
		else
			model.setViewName("redirect:/");
		return model;
	}
	
	@RequestMapping(value = "/editProduct", method = RequestMethod.POST)
	public ModelAndView editProduct (Product product, String page, HttpServletRequest request, HttpSession session){
		ModelAndView model = new ModelAndView();
		Member memberLogin = (Member)session.getAttribute("loginMember");		
		if(memberLogin!=null){
			product.setAccount(memberLogin.getAccount());
			ProductDAO productDAO = (ProductDAO)context.getBean("productDAO");
			productDAO.updateProduct(product);		
			model.setViewName("redirect:/showAllProductDescribe?p="+page);
		}
		else
			model.setViewName("redirect:/");
		return model;
	}
	
	@RequestMapping(value = "/delProduct", method = RequestMethod.GET)
	public @ResponseBody int delProductRevenueStructure (String  productID, String getPage, HttpServletRequest request , HttpSession session) throws ParseException{
		ModelAndView model = new ModelAndView();
		Member memberLogin = (Member)session.getAttribute("loginMember");		
		if(memberLogin!=null){	
			int returnPage = getPage!=null?Integer.parseInt(getPage):0;
			ProductDAO productDAO = (ProductDAO)context.getBean("productDAO");
			Product product = new Product();
			product.setAccount(memberLogin.getAccount());
			product.setId(productID);
			productDAO.delProduct(product);	
			
			ProductSalesDAO productSalesDAO = (ProductSalesDAO)context.getBean("productSalesDAO");
			ProductSales productSales = new ProductSales();
			productSales.setAccount(memberLogin.getAccount());
			productSales.setId(productID);
			productSalesDAO.delProductSalesByMember(productSales);
			
			int count = productDAO.countProductByMember(memberLogin);
			int page = count/10;
			System.out.println("page="+page);
			if(returnPage>page)
				returnPage=page;			
			System.out.println("returnPage2="+returnPage);
			return returnPage;
		}
		return 0;
	}
}
